package com.wky.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wky.book.common.context.UserTokenContextHolder;
import com.wky.book.common.exception.BasicInfoException;
import com.wky.book.common.exception.BasicInfoStatusEnum;
import com.wky.book.entity.BookCategory;
import com.wky.book.entity.Category;
import com.wky.book.enums.BookMoneyTypeEnum;
import com.wky.book.mapper.CategoryMapper;
import com.wky.book.request.CategoryQueryReqVo;
import com.wky.book.response.CategoryQueryResVo;
import com.wky.book.service.BookCategoryService;
import com.wky.book.service.CategoryService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wky.book.request.CategoryReqVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 分类表 服务实现类
 * </p>
 *
 * @author wky
 * @since 2022-04-02
 */
@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    BookCategoryService bookCategoryService;

    @Override
    @Transactional
    public void save(CategoryReqVo categoryReqVo) {
        // 获取用户id
        Long userId = UserTokenContextHolder.getUserTokenVOByToken().getUserId();
        // 判断是否重复名
        List<BookCategory> bookCategories = bookCategoryService.list(new QueryWrapper<BookCategory>().lambda().eq(BookCategory::getBookId, categoryReqVo.getBookId())).stream().collect(Collectors.toList());
        // 获取查询所有该账本的分类id
        List<Long> categoryIds = bookCategories.stream().map(BookCategory::getCategoryId).collect(Collectors.toList());
        if (null != categoryIds) {
            // 查询是否存在相同分类名称
            Category category = this.getOne(new QueryWrapper<Category>().lambda().in(Category::getId, categoryIds).eq(Category::getName, categoryReqVo.getName()));
            if (category != null) {
                throw new BasicInfoException(BasicInfoStatusEnum.SYSTEM_ERROR.getCode(),
                        "账本名称已经存在");
            }
        }
        Category category = new Category();
        BeanUtils.copyProperties(categoryReqVo, category);
        // 设置创建时间+创建人
        category.setCreateTime(new Date());
        category.setCreateBy(userId);
        category.setStatus("1");
        this.save(category);
        // 新增关系
        bookCategoryService.save(new BookCategory()
                .setCreateTime(new Date()).setCreateBy(userId)
                .setBookId(categoryReqVo.getBookId()).setCategoryId(category.getId())
                .setStatus("1"));
    }


    @Override
    public List<Category> findList(CategoryQueryReqVo categoryQueryReqVo) {
        return this.list(new QueryWrapper<Category>().lambda()
                .eq(Category::getBookId, categoryQueryReqVo.getBookId())
                .eq(Category::getStatus, "1")
                .eq(Category::getType, categoryQueryReqVo.getType()));
    }

    @Override
    public CategoryQueryResVo findBookIdList(Long bookId) {
        List<Category> list = this.list(new QueryWrapper<Category>().lambda()
                .eq(Category::getBookId, bookId)
                .eq(Category::getStatus, "1")
        );
        List<Category> srCategory = list.stream().filter(x -> BookMoneyTypeEnum.EXPENDITURE.getValue().intValue()==x.getType().intValue()).collect(Collectors.toList());
        List<Category> zcCategory = list.stream().filter(x -> BookMoneyTypeEnum.INCOME.getValue().intValue()==x.getType().intValue()).collect(Collectors.toList());
        return new CategoryQueryResVo(srCategory, zcCategory);
    }
}
